package org.openclinic.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.openclinic.domain.MedicalRecord;
import org.openclinic.exception.AbstractExceptionHandler;
import org.openclinic.service.MedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@SessionAttributes("medicalRecord")
public class MedicalRecordController extends AbstractExceptionHandler {

	@Autowired
	private MedicalRecordService medicalRecordService;
	
	@RequestMapping(value = "/mrecord.do", method = RequestMethod.GET)
	public ModelAndView init() {
		ModelAndView mav = new ModelAndView("medical-record.view");
		mav.addObject("currentDate", new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
		
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value = "/mrecord/history.action", method = RequestMethod.POST)
	public List<MedicalRecord> findByCustomer(@RequestParam long customerId) {
		return medicalRecordService.findByCustomer(customerId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/mrecord/save.action", method = RequestMethod.POST)
	public MedicalRecord save(@RequestParam long appointmentId, @RequestParam String description) {		
		return medicalRecordService.save(appointmentId, description);
	}
	
}
